﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LuckyLoot.ServiceFacade.Contracts;
using LuckyLoot.Domain.Entities;
using LuckyLoot.Common.Repositories;
using LuckyLoot.Common.Contexts;
using LuckyLoot.Common.Validation;

namespace LuckyLoot.ServiceFacade.Implementation
{
    public class ToolManagementService : IToolManagementService
    {
        public Tool CreateTool(string name, string description, DateTime? lastSterilization)
        {
            ValidState.Requires<ArgumentNullException>(!string.IsNullOrWhiteSpace(name));

            Tool tool = new Tool();
            tool.Name = name;
            tool.Description = description;
            tool.LastSterilization = lastSterilization;

            using (Repository<Tool, ApplicationContext> repository = new Repository<Tool, ApplicationContext>())
            {
                repository.Add(tool);
                repository.Save();
            }

            return tool;
        }

        public Tool UpdateTool(int toolID, string name, string description, DateTime? lastSterilization)
        {
            ValidState.Requires<ArgumentNullException>(toolID > 0);
            ValidState.Requires<ArgumentNullException>(!string.IsNullOrWhiteSpace(name));

            Tool tool = null;
            using (Repository<Tool, ApplicationContext> repository = new Repository<Tool, ApplicationContext>())
            {
                tool = this.GetTool(toolID);
                tool.Name = name;
                tool.Description = description;
                tool.LastSterilization = lastSterilization;

                repository.Edit(tool);
                repository.Save();
            }

            return tool;
        }

        public Tool SterilizeTool(int toolID)
        {
            var tool = SterilizeTool(toolID, DateTime.Now);

            return tool;
        }

        public Tool SterilizeTool(int toolID, DateTime sterilizationDate)
        {
            ValidState.Requires<ArgumentNullException>(toolID > 0);
            ValidState.Requires<ArgumentException>(sterilizationDate.Date <= DateTime.Today, "A data da última esterilização não pode ser uma data futura.");

            Tool tool = null;
            using (Repository<Tool, ApplicationContext> repository = new Repository<Tool, ApplicationContext>())
            {
                tool = this.GetTool(toolID);
                tool.LastSterilization = sterilizationDate;

                repository.Edit(tool);
                repository.Save();
            }

            return tool;
        }

        public Tool GetTool(int toolID)
        { 
            ValidState.Requires<ArgumentNullException>(toolID > 0);
            
            Tool tool = null;
            using (Repository<Tool, ApplicationContext> repository = new Repository<Tool, ApplicationContext>())
            {
                tool = repository.GetById(toolID);
                ValidState.Requires<InvalidOperationException>(tool != null, string.Format("Não foi possível encontrar a ferramenta de id {0}.", toolID));
            
            }

            return tool;
        }

        public List<Tool> GetAllTools()
        {
            List<Tool> tools = null;
            using (Repository<Tool, ApplicationContext> repository = new Repository<Tool, ApplicationContext>())
            {
                tools = repository.GetAll().ToList<Tool>();
            }

            return tools;
        }
    }
}
